export default {
	async getAddressList() {
		let res = await this.httptool.post(this.apis.addressList, this.list_params);
		this.tableData = res.data.list;
		for(var i = 0; i < this.tableData.length; i++) {
			if(this.tableData[i].defaultAddress == true) {
				this.radios = this.tableData[i].id;
				break
			} else {
				this.radios = '';
			}
		}
		console.log(this.radios);
	},
	async getAddressDetail(self, id, successBack, errorBack) {
		let params = {
			id: id
		};
		self.httptool.ajaxPost(self.apis.getAddressDetail, params, function(res) {
			self.params = res.data;
			if(self.addressInfo && self.addressInfo.latitude) {
				self.params.latitude = self.addressInfo.latitude;
				self.params.longitude = self.addressInfo.longitude;
				self.params.addressPre = self.addressInfo.address;
			}
		});
	},
	async delAddress(id) {
		let params = {
			id: id
		};
		//		this.$confirm('确定要删除吗？', '提示', {
		//			confirmButtonText: '确定',
		//			cancelButtonText: '取消',
		//			type: 'warning'
		//		}).then(() => {
		//			this.httptool.post(this.apis.delAddress, param, res=> {
		//				
		//				this.$message.success('删除成功！');
		//				this.getAddressList();
		//				
		//			});
		//		}).catch(() => {
		//
		//		});
		let res = await this.httptool.post(this.apis.delAddress, params);
		this.getAddressList();
	},
	async setDefaultAddr(id, status) {
		let defParams = {};
		defParams.id = id;
		defParams.pageNum = 1;
		defParams.pageSize = 10;
		if(status) {
			defParams.defaultAddress = false;
		} else {
			defParams.defaultAddress = true;
		}
		let res = await this.httptool.post(this.apis.setDefaultAddress, defParams);
		this.getAddressList();

	},
	async handleSave() {
		let that = this;
		let rule = [{
			name: 'name',
			label: '收货人',
			rule: ['validateEmpty_']
		}, {
			name: 'tel',
			label: '手机号',
			rule: ['validateTel_']
		}, {
			name: 'addressPre',
			label: '地址',
			rule: ['validateEmptyCity_']
		}, {
			name: 'addressDetail',
			label: '详细地址',
			rule: ['validateEmpty_']
		}]

		let rulesAddress = [{
			name: 'address',
			label: '地址',
			rule: ['validateEmpty_']
		}];
		if(!this.validatetool.validate(this, this.params, rule)) {
			return;
		}
		let requestUrl = this.apis.addAddress; //添加
		if(this.addrId) {
			requestUrl = this.apis.editAddress; //修改
		};
		let res = await this.httptool.post(requestUrl, this.params);
		if(requestUrl == this.apis.addAddress) {
			this.$message.success('添加成功！');
		} else {
			this.$message.success('编辑成功！');
		}
		this.params = {
			name: "", //姓名
			tel: "", //电话号码
			province: "", //省
			city: '', //市
			county: '', //县
			latitude: "", //经度
			longitude: "", //纬度
			addressPre: '', //省市
			addressDetail: "", //详细地址
			defaultAddress: true, //boolean类型,是否是默认地址:true表示是,false表示否
		}
		if(this.type == 'order') {
			this.$router.replace('/orderconfirm');
		} else {
			this.getAddressList();
			//			this.reload();
		}

	},
	async getDefaultAddress(self, id, successBack, errorBack) { //id>0,取对应的地址，id=0,取默认地址
		if(id) {
			let params = {
				id: id
			};
			self.httptool.ajaxPost(self.apis.getAddressDetail, params, function(res) {
				successBack(res.data);
			});
		} else {
			self.httptool.ajaxPost(self.apis.getDefaultAddress, {}, function(res) {
				successBack(res.data);
			});
		}
	}
}